#!/usr/bin/env python

import searchfs

import sys
import logging
import logging.config
import fuse
import gettext
from fuse import Fuse

import pkg_resources

gettext.install('searchfs')

def main():
    global server

    usage = """
mount_search: mounts a directory with the results of a query.

""" + Fuse.fusage

    server = searchfs.SearchFS(version="%prog " + fuse.__version__, 
                               usage=usage, 
                               dash_s_do='setsingle')

    server.parser.add_option(mountopt="conf",
                             metavar="CONF",
                             default='~/.searchfs/default.xml',
                             help=_("read configuration from CONF file [default: %default]"))
    server.parser.add_option(mountopt="provider",
                             metavar="PROVIDER",
                             default='Search',
                             help=_("use HANDLER to handle QUERY [default: %default]"))
    server.parser.add_option(mountopt="query",
                             metavar="QUERY",
                             default='locate *.mp3',
                             help=_("execute QUERY [default: %default]"))

    server.parse(values=server, errex=1)


    server.main()


if __name__ == '__main__':
    logging.config.fileConfig(pkg_resources.resource_filename('SearchFS', 'conf/logging.conf'))

    # redirect stdout to a disk file
    saveout = sys.stdout
    saveerr = sys.stderr
    outfile = open('/tmp/log.txt', 'a+')
    sys.stdout = outfile
    sys.stderr = outfile

    # output stuff
    main()

    # restore stdout
    outfile.flush()
    outfile.close()
    sys.stdout = saveout
    sys.stderr = saveerr

